<?php



namespace App\Services\Member\Services;


use App\Services\Member\Interfaces\RoleServiceInterface;
use App\Services\Member\Interfaces\UserServiceInterface;
use App\Services\Member\Interfaces\DeliverServiceInterface;



class DeliverService implements DeliverServiceInterface
{
    /**
     * @var UserService
     */
    protected $userService;
    /**
     * @var RoleService
     */
    protected $roleService;

    public function __construct(
        UserServiceInterface $userService,
        RoleServiceInterface $roleService
   ) {
        $this->userService = $userService;
        $this->roleService = $roleService;
    }


    /**
     * 会员套餐发货.
     *
     * @param array $product
     */
    public function deliverRole($userIds, array $product): bool
    {

        if (!is_array($userIds)) {
            $userIds = [$userIds];
        }
        foreach ($userIds as $userId){
            $userRole = $this->userService->findRole($userId,$product['goods_id']);
            $role = $this->roleService->find($product['goods_id']);

            if (!$userRole) {
                $this->roleService->userJoinRole($userId, $role, $role['charge'],$role['is_forever_membership']);
            } elseif (!$userRole['is_forever_membership']) {
                $this->roleService->userContinueRole($userId,$userRole,$role, $role['charge'],$role['is_forever_membership']);
            }

        }


        return  true;
    }
}
